import csv
from utils.consts import LOG_PATH


class Logger:
    def __init__(self, task_id: str) -> None:
        self.log_file = f"{LOG_PATH}/{task_id}.csv"
        self.first_write = True

    def log_outcome(self, outcomes: dict):
        # first time write need to create header
        if self.first_write:
            self.outcome_keys = [k for k in outcomes.keys()]
            with open(self.log_file, 'w') as f:
                writer = csv.DictWriter(f, fieldnames=self.outcome_keys)
                writer.writeheader()
                writer.writerow(outcomes)
            self.first_write = False
        else:
            with open(self.log_file, 'a') as f:
                writer = csv.DictWriter(f, fieldnames=self.outcome_keys)
                writer.writerow(outcomes)
